package com.soxing.braindata.common.util;

import com.jmatio.io.MatFileReader;
import com.jmatio.types.MLDouble;
import com.jmatio.types.MLStructure;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;


public class MatUtil {



    public static float read(String path,String unzipPath)
    {
        float QA = -1;
        try {
            int lastIndex = path.lastIndexOf('/');
            lastIndex = path.lastIndexOf('/', lastIndex - 1);
            String tmpdir = path.substring(lastIndex + 1);
            tmpdir = StringUtils.substringBeforeLast((unzipPath + tmpdir), "/");
            List<File> files = ZipUtil.upzipFile(path, tmpdir);
            for(File item:files)
            {
                if(item.getName().contains(".mat"))
                {
                    System.out.println(item.getAbsoluteFile());
                    MatFileReader reader = null;
                    try {
                        reader = new MatFileReader(item);
                        MLStructure EEG_results = (MLStructure) reader.getContent().get("results_QA");
                        MLDouble theta= (MLDouble)EEG_results.getField("ODQ");
                        double[][] temp = theta.getArray();

                        QA = (float) temp[0][0];
                        System.out.println(QA);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                }
                item.delete();
            }



        }catch(Exception e){
            e.printStackTrace();
        }



        return QA;

    }

    public static void main(String[] args)
    {
        read("D:/export/data/braindata//experiment/ffbbf59d479e42936393ff5926a44175/echa84_AATEST_EEG.zip","D:/export/data/unzip/");
        //read("C:\\Users\\Administrator\\Desktop\\DRT_Application\\javatest2\\e8fajs_CompareNumber_EEG.zip","D:/export/data/unzip/");
    }
}
